﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using System.IO;

namespace Chapter04
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            var counter = Server.MapPath("Example110.Counter.txt");
            Application["counter"] = File.ReadAllText(counter);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Application.Lock();
            var obj = Application["counter"];
            if (string.IsNullOrEmpty(obj.ToString()))
            {
                obj = 0;
            }
            Application["counter"] = int.Parse(obj.ToString()) + 1;
            File.WriteAllText(Server.MapPath("Example110.Counter.txt"), Application["counter"].ToString());
            Application.UnLock();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            File.WriteAllText(Server.MapPath("Example110.Counter.txt"), Application["counter"].ToString());
        }
    }
}